package com.coder.bike.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.Role;
import com.coder.bike.service.IPermissionService;
import com.coder.bike.service.IRoleService;
import com.coder.bike.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/bike/role")
public class RoleController {
    @Resource
    private IRoleService roleService;
    @Resource
    private IPermissionService permissionService;
    @GetMapping
    public Result list() {
        return Result.success(roleService.list());
    }
    @PostMapping("{start}/{size}")
    public Result search(@PathVariable Integer start,
                         @PathVariable Integer size,
                         @RequestBody Role role) {
        Page<Role> page = new Page<>(start, size);
        return Result.success(roleService.selectList(page, role));
    }
    //获取当前用户所绑定的角色id
    @GetMapping("role/{userId}")
    public Result selectRoleByUserId(@PathVariable Integer userId){
        return Result.success(roleService.selectRoleIdByUserId(userId));
    }

    @GetMapping("bind/{userId}/{roleIds}")
    public Result bindRole(@PathVariable Integer userId,
                           @PathVariable String roleIds){
        List<Integer> list = Arrays.stream(roleIds.split(","))
                .map(Integer::parseInt)
                .toList();
        return roleService.bindRole(userId,list)?
                Result.success():Result.fail();
    }
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:add')")
    public Result save(@RequestBody Role role) {
        return roleService.save(role) ? Result.success() : Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody Role role) {
        return roleService.updateById(role) ? Result.success() : Result.fail();
    }

    @GetMapping("hasUser/{id}")
    public Result hasUser(@PathVariable Integer id) {
        return roleService.hasUser(id) ? Result.success().setMessage("有") :
                Result.success().setMessage("无");
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        return roleService.delete(ids) ? Result.success() : Result.fail();
    }

    //获取菜单树结构 当前角色id,  用户id
    @GetMapping("permissionTree")
    public Result selectPermissionTree(Integer userId, Integer roleId) {
        return Result.success(permissionService.selectPermissionTree(userId, roleId));
    }

    //权限分配
    @PostMapping("/assign")
    public Result assignPermission(
            @RequestParam int roleId,
            @RequestParam String permissionIds) {
        List<Integer> list = Arrays.stream(permissionIds.split(","))
                .map(Integer::parseInt)
                .toList();
        return roleService.assignPermission(roleId, list) ?
                Result.success() : Result.fail();
    }
}
